﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using CWebSocket;
using SocketWebTest.webToNative;

namespace SocketWebTest
{
    public partial class Form1 : Form
    {


        /// <summary>
        /// 获取窗体的句柄函数
        /// </summary>
        /// <param name="lpClassName">窗口类名</param>
        /// <param name="lpWindowName">窗口标题名</param>
        /// <returns>返回句柄</returns>
        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        /// <summary>
        /// 移动窗体的函数
        /// </summary>
        /// <param name="hWnd">窗体句柄</param>
        /// <param name="x">窗体新位置x轴坐标</param>
        /// <param name="y">窗体新位置y轴坐标</param>
        /// <param name="nWidth">窗体新位置宽度</param>
        /// <param name="nHeight">窗体新位置高度</param>
        /// <param name="BRePaint">是否刷新窗体</param>
        /// <returns></returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool BRePaint);

        /// <summary>
        /// 通过句柄，窗体显示函数
        /// </summary>
        /// <param name="hWnd">窗体句柄</param>
        /// <param name="cmdShow">显示方式</param>
        /// <returns>返工成功与否</returns>
        [DllImport("user32.dll", EntryPoint = "ShowWindowAsync", SetLastError = true)]
        public static extern bool ShowWindow(IntPtr hWnd, int cmdShow);


        /// <summary>
        /// 发送消息到窗体函数
        /// </summary>
        /// <param name="hwnd">窗体句柄</param>
        /// <param name="wMsg">消息类型</param>
        /// <param name="wParam">附带消息</param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);


        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern System.IntPtr GetForegroundWindow();  //获取当前窗口句柄


        //wMsg类型，关闭窗体
        private const int WM_CLOSE = 0x10;


        public Form1()
        {
            InitializeComponent();

            WebSocket.Instance.start();

            HandlerHelper.RegisterHandler("form", new FormOperaHandler());

            InitDefaultForm();

            getChromeProcess();

        }

        public void getChromeProcess()
        {
            Process[] allProgresse = System.Diagnostics.Process.GetProcessesByName("chrome");
            foreach (Process closeProgress in allProgresse)
            {
                if (closeProgress.ProcessName.IndexOf("chrome") > -1)
                {
                    //closeProgress.Kill();
                    //closeProgress.WaitForExit();
                }
            }
        }

        public void InitDefaultForm()
        {
            var screenWidth = Screen.PrimaryScreen.Bounds.Width;
            var screenHeight = Screen.PrimaryScreen.Bounds.Height;
            this.Left = 200;
            this.Top = 100;

            this.BackColor = Color.LightPink;

            this.BackColor = Color.White;
            //this.TransparencyKey = Color.White;
            this.Opacity = 1;


            //置顶显示
            this.TopMost = true;

            //无边框
            //this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //IntPtr textHandle = FindWindow(null, "测试文本.txt - 记事本");'
            IntPtr textHandle = FindWindow("Chrome_WidgetWin_0",null);
            if (textHandle != IntPtr.Zero)
            {

                
                //移动记事本窗体
                MoveWindow(textHandle, 0, 100, 800, 600, true);
                //延时3秒后
                //Thread.Sleep(3000);
                //设置窗体最大化显示
                //ShowWindow(textHandle, 3);
                //延时3秒后
                Thread.Sleep(3000);
                //向记事本程序窗体发送关闭信息
                //SendMessage(textHandle, WM_CLOSE, 0, 0);
            }
        }
    }


}
